#include "my_header.h"
#include "threadPool.h"
int exitPipe[2];//handler只能访问全局的数据
void handler(int signum){
    printf("Parent got signal, signum = %d\n", signum);
    write(exitPipe[1],"1",1);
}
int main(int argc,char*argv[])
{
    ARGS_CHECK(argc,4);
    pipe(exitPipe);
    if(fork()){
        close(exitPipe[0]);//父进程关闭管道读端
        signal(SIGUSR1,handler);
        wait(NULL);
        DEBUG_PRINT("Parent is going to exit\n");
        exit(0);
    }
    close(exitPipe[1]);
    int workerNum=atoi(argv[3]);
    //创建并初始化线程数组
    threadPool_t threadPool;
    int ret=threadPoolInit(&threadPool,workerNum);
    if(ret!=0){
        DEBUG_PRINT("threadPoolInit fail\n");
    }
    // 添加TCP初始化测试打印
    DEBUG_PRINT("Testing tcpInit...\n");
    int sockfd=tcpInit(argv[1],argv[2]);
    ERROR_CHECK(sockfd,-1,"sockfd");
    DEBUG_PRINT("tcpInit returned: %d\n", sockfd);
    if (sockfd <= 0) {
        DEBUG_PRINT("TCP socket initialize failure\n");
    }
    DEBUG_PRINT("Server is running...\n");

    //进行监听
    int epfd=epoll_create(1);
    ret=epollAdd(epfd,sockfd);
    if (ret == -1) {
        // 如果添加失败，打印错误信息
        DEBUG_PRINT("Failed to add main socket fd %d to epoll instance %d\n", sockfd,epfd);
        perror("epollAdd");
        // 这里可以根据实际情况添加错误处理逻辑，比如退出程序
    } else {
        // 如果添加成功，打印成功信息
        DEBUG_PRINT("Successfully added main socket fd %d to epoll instance %d\n", sockfd,epfd);
    }
    ret =epollAdd(epfd,exitPipe[0]);
    if(ret!=-1){
        DEBUG_PRINT("Child successfullly add exitPipe[0] to epoll instance\n");
    }
    ret=makeWorker(&threadPool);
    if(ret==0){
        DEBUG_PRINT("success makeworker\n");
    }
    struct epoll_event readyset[1024];
    while(1){
        DEBUG_PRINT("Before epoll_wait\n");
        int readynum=epoll_wait(epfd,readyset,1024,-1);
        DEBUG_PRINT("Epoll_wait return ,readynum=%d\n",readynum);
        for(int i=0;i<readynum;++i){
            if(readyset[i].data.fd==sockfd){
                int netfd=accept(sockfd,NULL,NULL);
                DEBUG_PRINT("Accepted connection: fd=%d\n", netfd);
                pthread_mutex_lock(&threadPool.mutex);
                enQueue(&threadPool.taskQueue,netfd);//入队
                DEBUG_PRINT("add netfd %d to taskQuene\n",netfd);
                pthread_cond_broadcast(&threadPool.cond);
                DEBUG_PRINT("main broadcast all workers\n");
                pthread_mutex_unlock(&threadPool.mutex);
            }
            else if(readyset[i].data.fd==exitPipe[0]){
                DEBUG_PRINT("threadPool is going to exit\n");
                pthread_mutex_lock(&threadPool.mutex);
                threadPool.exitFlag=1;
                pthread_cond_broadcast(&threadPool.cond);
                DEBUG_PRINT("main brocast all workers\n");
                pthread_mutex_unlock(&threadPool.mutex);
                for(int i=0;i<workerNum;++i){
                    pthread_join(threadPool.tidArr.arr[i],NULL);
                }
                DEBUG_PRINT("Master is going to exit\n");
                exit(0);
            }
        }
    }
    return 0;
}
